#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <syslog.h>
#include <errno.h>
#include <stdlib.h>
#define MAX_LISTEN_NUM 5
#define SEND_BUF_SIZE 100
#define RECV_BUF_SIZE 100
#define SERVER_PORT 8889
int main()
{
    int sock_fd = 0;
    char recvbuf[RECV_BUF_SIZE] = {0};
    char sendbuf[SEND_BUF_SIZE] = {0};
    int recvlen = 0;
    int retlen = 0;
    int sendlen = 0;
    int leftlen = 0;
    char *ptr = NULL;
    struct sockaddr_in ser_addr;

    memset(&ser_addr, 0, sizeof(ser_addr));
    ser_addr.sin_family = AF_INET;
    ser_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    ser_addr.sin_port = htons(SERVER_PORT);
    sock_fd = socket(AF_INET, SOCK_STREAM, 0);
    connect(sock_fd, (struct sockaddr *)&ser_addr, sizeof(ser_addr));
    //receive data
    recvlen = 0;
    retlen = 0;
    ptr = recvbuf;
    leftlen = RECV_BUF_SIZE -1;
    //do
    {
        retlen = recv(sock_fd, ptr, leftlen, 0) ;
        if(retlen < 0)
        {
            if(errno == EINTR)
                retlen = 0;
            else
                exit(1);
        }
        recvlen += retlen;
        leftlen -= retlen;
        ptr += retlen;
    }
    //while(recvlen && leftlen);
    printf("receive data is : %s\n", recvbuf);
    sprintf(sendbuf, "hello server");
    //send data
    sendlen = strlen(sendbuf) +1;
    retlen = 0;
    leftlen = sendlen;
    ptr = sendbuf;
    //while(leftlen)
    {
        retlen = send(sock_fd, ptr, sendlen, 0);
        if(retlen < 0)
        {
            if(errno == EINTR)
                retlen = 0;
            else
                exit(1);
        }
        leftlen -= retlen;
        ptr += retlen;
    }
    printf("客户端已向服务器发送信息\n");
    close(sock_fd);

}
